<template>
    <view style="height: 86vh">
        <scroll-view scroll-y style="height: 86vh">
            <view class="list">
                <view class="item" v-for="(item, index) in list" :key="index">
                    <view class="left">
                        <view>{{ item.phone_number}} {{ item.real_name}} 收</view>
                        <view>{{item.province}}{{item.city}}{{item.district}}{{ item.full_address }}</view>
                    </view>

                    <view class="edit" @click="toAddressadd(item)">
                        <image src="/static/images/edit.png"></image>
                    </view>

                    <view class="choice" v-if="item.id != id" @tap="defaultFun(item)">
                        <image src="/static/images/choice_n.png"></image>
                    </view>

                    <view class="choice" v-else>
                        <image src="/static/images/choice_y.png"></image>
                    </view>
                </view>
            </view>
        </scroll-view>
        <navigator url="/pages/addressAdd/addressAdd" class="btn">新增收货地址</navigator>
    </view>
</template>

<script>
// pages/addressList/addressList.js
const app = getApp();
export default {
    data() {
        return {
            list: [],
            id: null,
            state: null
        };
    },
    onLoad: function (options) {
	const userid=uni.getStorageSync("userid");
        uni.request({
			url:"/api/user/address/getAddress",
			method:"POST",
			data:{user_id:userid},
                success: (res) => {
                    console.log("查询成功",res)
                    this.setData({
				
                        list: res.data.addressList,
                        state: options.state
                    });
                }
            });
    },
    methods: {
		toAddressadd:function(item){
			uni.navigateTo({
				url:"../addressAdd/addressAdd?city="+item.province+item.city+item.district+"&mobile="+item.phone_number+"&name="+item.real_name+"&detailed="+item.full_address+"&checked=1",
			});
		},
        defaultFun: function (item) {
            console.log(item);
            var that = this;
            app.globalData.address =item.region.join('');
            app.globalData.addressDetail =item.region.join('') + item.full_address;
            app.globalData.phone_number = item.phone_number;
            app.globalData.real_name = item.real_name; // console.log(app.globalData.username+"2")

            that.setData({
                id: item.id
            });

            if (that.state == 1) {
                uni.navigateBack({
                    delta: 1
                });
            }
        }
    }
};
</script>
<style>
.list .item {
    display: flex;
    margin: 0 18px;
    border-bottom: 1px solid #dedede;
    padding: 15px 0;
    align-items: center;
    height: 98px;
}
.item .left {
    flex: 1 0 80%;
    font-size: 14px;
}
.item .edit,
.choice {
    flex: 1 0 10%;
    text-align: center;
}
.item .edit image {
    width: 20px;
    height: 20px;
}
.item .choice image {
    width: 20px;
    height: 20px;
}

.btn {
    height: 8vh;
    line-height: 8vh;
    text-align: center;
    background: #ef9b9b;
    font-size: 14px;
    color: #fff;
}
</style>
